/**!
 *
 * 添加学习动态。
 *
 */

$(function () {
    /** 动态表单的 jQuery 对象 */
    var $learnForm = $("#learn_form");
    /** 动态附件名称的 jQuery 对象 */
    var $learnFileNames = $learnForm.find("input[name='learnFileNames']");
    /** 动态附件路径的 jQuery 对象 */
    var $learnFilePaths = $learnForm.find("input[name='learnFilePaths']");
    /** 上传动态附件的 jQuery 对象 */
    var $uploadLearnFilesBtn = $("#upload_learn_files_btn");
    /** 显示校验信息的容器 */
    var $validatecontainer = $("#validate_container");
    /** 添加按钮的 jQuery 对象 */
    var $addLearnBtn = $learnForm.find("button[name='addLearnBtn']");
    /** 动态附件的名称 */
    var learnFileNames = "";
    /** 动态附件的路径 */
    var learnFilePaths = "";

    // 上传附件
    var $learnFilesUploadifive = $uploadLearnFilesBtn.myUploadifive({
        auto: false,
        multi: true,
        width: 80,
        height: 30,
        queueID: "learn_queue",
        buttonText: "<i class='ace-icon iconfont icon-upload f-s-12'></i>上传附件",
        buttonClass: "btn btn-success btn-xs",
        fileObjName: "file",
        uploadScript: "{0}web/file/upload.json".format(globalBasePath),
        fileSizeLimit: String(50 * 1024),
        onUploadFn: function () {
            $learnFilesUploadifive.uploadifiveData.settings.formData.path = "/upload/learn_dym";
        },
        onUploadCompleteFn: function (file, data) {
            var result = data.result;

            learnFileNames += (result.fileName + "|");
            learnFilePaths += (result.filePath + "|");
        },
        onQueueCompleteFn: function () {
            $learnFileNames.val(learnFileNames.substring(0, (learnFileNames.length - 1)));
            $learnFilePaths.val(learnFilePaths.substring(0, (learnFilePaths.length - 1)));

            // 提交表单
            $learnForm.get(0).submit();
        }
    });

    $learnForm.validate({
        rules: {
            learnTitle: {
                required: true,
                maxlength: 500
            },
            learnContent: {
                required: true,
                maxlength: 2000
            },
            learnLevel: {
                required: true
            }
        },
        messages: {
            learnTitle: {
                required: "<span class='f-s-12'><i class='iconfont icon-error f-s-12'></i>请输入动态标题</span>",
                maxlength: "<span class='f-s-12'><i class='iconfont icon-error f-s-12'></i>动态标题的长度不能大于{0}个字符</span>"
            },
            learnContent: {
                required: "<span class='f-s-12'><i class='iconfont icon-error f-s-12'></i>请输入动态内容</span>",
                maxlength: "<span class='f-s-12'><i class='iconfont icon-error f-s-12'></i>动态内容的长度不能大于{0}个字</span>"
            },
            learnLevel: {
                required: "<span class='f-s-12'><i class='iconfont icon-error f-s-12'></i>请选择动态级别</span>"
            }
        },
        errorContainer: $validatecontainer,
        errorLabelContainer: $("ul", $validatecontainer),
        wrapper: "li",
        submitHandler: function (form) {
            $addLearnBtn.html("<i class='ace-icon fa fa-spinner fa-spin bigger-150'></i>正在提交…").attr({"disabled": "disabled"});

            if ($.isNotNull($learnFilesUploadifive) && ($learnFilesUploadifive.uploadifiveData.queue.count > 0)) {
                // 开始上传文件
                $uploadLearnFilesBtn.uploadifive("upload");
            } else {
                // 提交表单
                $learnForm.get(0).submit();
            }

            return false;
        }
    });
});